package com.itouch.bracelet.activity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.itouch.util.JsonObjectPostRequest;
import com.itouch.util.LoadingDialog;

/**
 * 
 * @author shenjun (385811416@qq.com)
 * 
 */
public abstract class BaseActivity extends SherlockFragmentActivity {

	public Context context;

	private LoadingDialog loadingDialog;

	public void back(View view){
		
		this.finish();
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置标题栏无
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(setContentViewById());
		if(getSupportActionBar()!=null){
			getSupportActionBar().hide();
		}
		context = this;
		initViews();
		initData();
	}

	/**
	 * 设置视图的资源id (example:R.layout.xxx)
	 * 
	 * @return 资源id
	 */
	protected abstract int setContentViewById();

	/**
	 * 初始化资�?
	 */
	protected abstract void initViews();

	/**
	 * 初始化数
	 */
	protected abstract void initData();

	/**
	 * 显示进度加载
	 */
	public void show() {
		if (loadingDialog == null) {
			loadingDialog = LoadingDialog.createDialog(this);
			// 点击外部区域，是否消失loadingDialog
			loadingDialog.setCanceledOnTouchOutside(false);
		}
		loadingDialog.setMessage("正在加载");
		loadingDialog.show();
	}

	/**
	 * 关闭进度加载
	 */
	public void disMiss() {
		if (loadingDialog != null) {
			loadingDialog.dismiss();
			loadingDialog = null;
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		// 关闭进度加载
		disMiss();
	}

	/**
	 * get
	 * 
	 * @param url
	 * @param map
	 * @method
	 * 
	 * 
	 *         // Uri.Builder builder = Uri.parse(url).buildUpon(); //
	 *         builder.appendQueryParameter("key", "value"); //
	 *         builder.toString();
	 * 
	 * 
	 */

	public void getJSONByVolleyGET(String url,
			Response.Listener<JSONObject> l1, Response.ErrorListener l2) {
		RequestQueue requestQueue = Volley.newRequestQueue(this);
		JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
				Request.Method.GET, url, null, l1, l2);
		requestQueue.add(jsonObjectRequest);
	}

	/**
	 * post 
	 * 
	 * @param url
	 * @param map
	 * @method
	 */

	public void getJSONByVolleyPOST(String url, Map<String, String> map,
			Response.Listener<JSONObject> l1, Response.ErrorListener l2) {
		RequestQueue requestQueue = Volley.newRequestQueue(this);
		JsonObjectPostRequest jsonObjectRequest = new JsonObjectPostRequest(
				url, l1, l2, map);
		requestQueue.add(jsonObjectRequest);
	}
	public String getTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd日-HH时:mm分");
		return sdf.format(new Date());
	}
}
